package pl.com.like.sequoia.web.beans.view;

import java.awt.Color;
import java.io.Serializable;
import java.text.MessageFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import pl.com.like.sequoia.model.bio.BioObjectAlign;
import pl.com.like.sequoia.model.bio.BioObjectHandler;
import pl.com.like.sequoia.web.beans.search.AbstractObjectBean;
import pl.com.like.sequoia.web.util.JsfUtils;
import pl.com.like.sequoia.web.vo.PdbObjectDataHandler;

public class ComparisonObjectBean extends AbstractObjectBean implements Serializable {

	private static final long serialVersionUID = 1092554950955018708L;
	private static final Logger log = LoggerFactory.getLogger(ComparisonObjectBean.class);
	private String objectName;
	private PdbObjectDataHandler parentObj;
	private PdbObjectDataHandler childObj;
	private String extraScript;
	private String objectString;
	private BioObjectAlign result;

	public String createRasmolScripts(List<String> align) {
		String[] scripts = new String[2];

		Color col1 = Color.red;
		Color col2 = Color.blue;

		Color chaincol1 = new Color(col1.getRed() / 2, col1.getGreen() / 2, col1.getBlue() / 2);
		Color chaincol2 = new Color(col2.getRed() / 2, col2.getGreen() / 2, col2.getBlue() / 2);

		String cmd1 = "model 0;";
		String cmd2 = "";

		cmd1 += "select */1; ";
		cmd1 += " color [" + chaincol1.getRed() + "," + chaincol1.getGreen() + "," + chaincol1.getBlue() + "];";

		cmd2 += "select */2; ";
		cmd2 += " color [" + chaincol2.getRed() + "," + chaincol2.getGreen() + "," + chaincol2.getBlue() + "];";

		cmd1 += "select ";
		cmd2 += "select ";

		String pdbAchain = align.get(0);
		String pdbBchain = align.get(1);
		// align is written in DB as comma delimited atoms
		String[] pdb1s = StringUtils.commaDelimitedListToStringArray(pdbAchain);
		String[] pdb2s = StringUtils.commaDelimitedListToStringArray(pdbBchain);

		for (int i = 0; i < pdb1s.length; i++) {

			String p1 = pdb1s[i];
			String p2 = pdb2s[i];

			cmd1 += p1 + "/1";
			cmd2 += p2 + "/2";

			if (i <= pdb1s.length - 2) {
				cmd1 += ",";
				cmd2 += ",";
			}
		}

		cmd1 += "; color [" + col1.getRed() + "," + col1.getGreen() + "," + col1.getBlue() + "];";
		cmd1 += " backbone 0.6;";

		cmd2 += "; color [" + col2.getRed() + "," + col2.getGreen() + "," + col2.getBlue() + "];";
		cmd2 += " backbone 0.6;";

		// System.out.println(cmd1);
		scripts[0] = cmd1;
		scripts[1] = cmd2;
		// change to 1 row of query
		return StringUtils.arrayToDelimitedString(scripts, ";");
	}

	public PdbObjectDataHandler getChildObj() {
		if (childObj == null) {
			String tempObj = getObjectName();
			if (tempObj != null && !tempObj.isEmpty()) {
				List<BioObjectHandler> list = getPdbService().getComparisonPairs(tempObj);
				if (list != null && !list.isEmpty()) {
					BioObjectHandler child = list.get(1);
					childObj = new PdbObjectDataHandler(child);
				} else {
					String msg = MessageFormat.format("for given object {0} could not found child object", tempObj);
					log.warn(msg);
					JsfUtils.addWarnMessage(msg);
				}
			}
		}
		return childObj;
	}

	public String getExtraScript() {
		if (StringUtils.hasLength(getObjectName())) {
			List<String> align = getPdbService().getExtraRasmolScript(getObjectName());
			extraScript = createRasmolScripts(align);
		} else {
			extraScript = "";
		}
		return extraScript;
	}

	public String getObjectName() {
		return objectName;
	}

	public String getObjectString() {
		if (getParentObj() != null && getChildObj() != null) {
			objectString = getPdbService().getPdbObjectString(getParentObj().getName(), getChildObj().getName());
		} else {
			objectString = "";
		}
		return objectString;
	}

	public PdbObjectDataHandler getParentObj() {
		if (parentObj == null) {

			String tempObj = getObjectName();
			if (StringUtils.hasLength(tempObj)) {
				List<BioObjectHandler> list = getPdbService().getComparisonPairs(tempObj);
				if (list != null && !list.isEmpty()) {
					BioObjectHandler parent = list.get(0);
					parentObj = new PdbObjectDataHandler(parent);
				} else {
					String msg = MessageFormat.format("for given object {0} could not found parent object", tempObj);
					log.warn(msg);
					JsfUtils.addWarnMessage(msg);
				}
			}
		}
		return parentObj;
	}

	public BioObjectAlign getResult() {
		if (result == null) {
			String tempObj = getObjectName();
			result = getPdbService().getObjectAlign(tempObj);
		}
		return result;
	}

	public void setExtraScript(String extraScript) {
		this.extraScript = extraScript;
	}

	public void setObjectName(String objectName) {
		this.objectName = objectName;
	}

	public void setObjectString(String objectString) {
		this.objectString = objectString;
	}

	public void setResult(BioObjectAlign results) {
		this.result = results;
	}

}
